/**********************************************************************
Copyright (c) 2014-2016 Mark C. Erickson

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANT-
ABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CHANGE LOG:
v1.1.0 - added GUI
v1.0.1 - initial release

**********************************************************************/

desc:Knob: Wide [±12dB], v1.0.1

slider1: 50 <0, 100, 0.1> Position [mm]

@init
gfx_clear = 256*256*256-1;
m = 1;

@slider
c = 2 * ((slider1 / 100.0) - 0.5);  // -1 < c < 1
d = sqrt(abs(c)) * 12;
c < 0 ? d *= -1; 
m = pow( 10, d / 20.0 );
   
@sample
spl0 *= m;
spl1 *= m;


@gfx 256 256
gfx_setfont(1, "Arial", 10);
gfx_a = 1;

gfx_r = gfx_g = gfx_b = 0.1;
gfx_line(0, 0, gfx_w, 0, 0.5);  //  outline our GUI box
gfx_line(0, 0, 0, gfx_h, 0.5);
gfx_line(gfx_w-1, 0, gfx_w-1, gfx_h, 0.5);
gfx_line(0, gfx_h-2, gfx_w, gfx_h-2, 0.5);

x_step = gfx_w / 10;
y_step = gfx_h / 10;

i = 1;
loop(9,                         //  y-axiis
  x = i*x_step;
  gfx_line(x, y_step, x, y_step*7, 0.5);
  i += 1;
);
  
gfx_x = x_step - 2;
gfx_y = gfx_h - 3*y_step + 9;
gfx_drawchar($'0');
gfx_drawchar($'m');gfx_drawchar($'m'); 
  
gfx_x = 3*x_step - 7;
gfx_y = gfx_h - 3*y_step + 9;
gfx_drawchar($'2');
gfx_drawchar($'5');
gfx_drawchar($'m');gfx_drawchar($'m'); 
  
gfx_x = 5*x_step - 7;
gfx_y = gfx_h - 3*y_step + 9;
gfx_drawchar($'5');
gfx_drawchar($'0');
gfx_drawchar($'m');gfx_drawchar($'m'); 
  
gfx_x = 7*x_step - 7;
gfx_y = gfx_h - 3*y_step + 9;
gfx_drawchar($'7');
gfx_drawchar($'5');
gfx_drawchar($'m');gfx_drawchar($'m'); 

gfx_x = 9*x_step - 12;
gfx_y = gfx_h - 3*y_step + 9;
gfx_drawchar($'1');
gfx_drawchar($'0');
gfx_drawchar($'0');
gfx_drawchar($'m');gfx_drawchar($'m');   

i = 1;
loop(7,                         //  x-axiis
  y = i*y_step;
  gfx_line(x_step-2, y, x_step*9, y, 0.5);
  i += 1;
);

gfx_x = x_step - 38;
gfx_y = y_step - 6;
gfx_drawchar($'+');
gfx_drawchar($'1');
gfx_drawchar($'2');
gfx_drawchar($'d');
gfx_drawchar($'B');

gfx_x = x_step - 36;
gfx_y = 2*y_step - 7;
gfx_drawchar($'+');
gfx_drawchar($' ');
gfx_drawchar($'8');
gfx_drawchar($'d');
gfx_drawchar($'B');

gfx_x = x_step - 36;
gfx_y = 3*y_step - 7;
gfx_drawchar($'+');
gfx_drawchar($' ');
gfx_drawchar($'4');
gfx_drawchar($'d');
gfx_drawchar($'B');

gfx_x = x_step - 32;
gfx_y = 4*y_step - 7;
gfx_drawchar($' ');
gfx_drawchar($' ');
gfx_drawchar($'0');
gfx_drawchar($'d');
gfx_drawchar($'B');

gfx_x = x_step - 36;
gfx_y = 5*y_step - 7;
gfx_drawchar($'-');
gfx_drawchar($' ');
gfx_drawchar($'4');
gfx_drawchar($'d');
gfx_drawchar($'B');

gfx_x = x_step - 36;
gfx_y = 6*y_step - 7;
gfx_drawchar($'-');
gfx_drawchar($' ');
gfx_drawchar($'8');
gfx_drawchar($'d');
gfx_drawchar($'B');

gfx_x = x_step - 38;
gfx_y = 7*y_step - 7;
gfx_drawchar($'-');
gfx_drawchar($'1');
gfx_drawchar($'2');
gfx_drawchar($'d');
gfx_drawchar($'B');

gfx_r = gfx_g = 0.1;
gfx_b = 0.9;
dB = 20.01 * log10(m) + 12;
y = y_step*7 - y_step*dB/4;// + y_step;
gfx_rect(x_step+2, y, x_step*8-3, 2, 0.5);          //  horizontal line
gfx_rect(x_step+slider1/100*x_step*8, y_step+2, 1, y_step*6-2, 0.5);//  vertical line
gfx_rect(x_step+slider1/100*x_step*8-3, y-2, 6, 6, 0.5);  //  hilite box
gfx_x = gfx_w*.9+8;
gfx_y = y-6;
sprintf(str, "%.1fdB", dB-12);
gfx_drawstr(str);
gfx_x = x_step+slider1/100*x_step*8-9;
gfx_y = y_step - 17;
sprintf(str, "%.1fmm", slider1);
gfx_drawstr(str);

gfx_g = 0.2;
gfx_b = gfx_r = 0.9;
RES = 100;                              //  min = 8
x_step1 = x_step * 8/RES;

i = 1;
loop(RES,  //8,                         //  main curve
  x = i*x_step1;
  
  c=(i-1)/RES;
  d = 2 * ((c) - 0.5); 
  dB = sqrt(abs(d)) * 12;
  d < 0 ? dB *= -1; 
  dB += 12;
  y = y_step*7 - y_step*dB/4;
  
  c=(i)/RES;
  d = 2 * ((c) - 0.5);  
  dB1 = sqrt(abs(d)) * 12; 
  d < 0 ? dB1 *= -1; 
  dB1 += 12;
  y1 = y_step*7 - y_step*dB1/4;
  
  gfx_line(x_step+x-x_step1, y, x_step+x, y1, 0.5);
  i += 1;
);

gfx_setfont(1, "Arial", 12);            //  main title
gfx_r = gfx_g = gfx_b = 0.0;
gfx_x = x_step;
gfx_y = gfx_h - y_step - 7;
gfx_drawstr("Wide Knob:  Gain (dB) - vs - Fader Travel (mm)");

// mouse
mouse_cap == 1 ? (
  xx = mouse_x;
  xx > gfx_w * 0.1 ? (
    xx < gfx_w * 0.9 ? (
      yy = mouse_y;
      yy > gfx_h * 0.1 ? (
        yy < gfx_h * 0.7 ? (
          slider1 = 100 * (xx - gfx_w*.1) / (gfx_w*.9 - gfx_w*.1);
          slider1 < 0.3 ? slider1 = floor(slider1);
          slider1 > 99.7 ? slider1 = ceil(slider1);
          slider1 = floor(slider1*10);
          slider1 /= 10;
          sliderchange(slider1);
          slider_automate(slider1);
          c = 2 * ((slider1 / 100.0) - 0.5);  // -1 < c < 1
		  d = sqrt(abs(c)) * 12;
		  c < 0 ? d *= -1; 
		  m = pow( 10, d / 20.0 );
        )
      );
    );
  );
);
